<template>
    <Row>
        <Col span="22" offset="1">
            <Form ref="AppSecretManage" :model="AppSecretManage" :rules="ruleValidate" :label-width="120" :style="{marginTop: '30px'}">
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="应用编码">
                            <Input v-model.trim="AppSecretManage.NumCode" readonly placeholder="应用编码由系统自动生成" ></Input>
                        </FormItem>
                    </Col>
                </Row>
                
                <Divider orientation="left">应用基本信息</Divider>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="App供应商" prop="Name">
                            <Input  v-model.trim="AppSecretManage.Name" placeholder="例如：微信小程序" ></Input>
                        </FormItem>
                    </Col>
                    <Col span="10">
                        <FormItem label="应用名称" prop="AppName">
                            <Input v-model.trim="AppSecretManage.AppName" placeholder="例如：运营中心公告栏" ></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="AgentID" prop="AgentID">
                            <Input v-model.trim="AppSecretManage.AgentID" placeholder="例如：1000052"></Input>
                        </FormItem>
                    </Col>
                    <Col span="10">
                        <FormItem label="AppKey" prop="AppKey">
                            <Input v-model.trim="AppSecretManage.AppKey" placeholder="例如：asdfad545142dfd"></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="20" offset="2">
                        <FormItem label="AppSecret" prop="AppSecret">
                            <Input v-model.trim="AppSecretManage.AppSecret" placeholder="例如：asdfad545142dfd"></Input>
                        </FormItem>
                    </Col>
                </Row>
                
                <Divider orientation="left">其他信息</Divider>
                <Row>
                    <Col span="10" offset="2">
                        <FormItem label="启用标记">
                            <Select v-model="AppSecretManage.IsUsed" >
                                <Option v-for="item in IsUsed" :value=item.value :key="item.label">{{ item.label }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <Col span="10" >
                        <FormItem label="创建时间">
                            <DatePicker v-model="AppSecretManage.CreateTime" type="date" placeholder="请选择日期" format="yyyy年MM月dd日 HH时mm分ss秒" readonly style="width:100%"></DatePicker>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span="20" offset="2" style="text-align:center;margin-top:58px">
                        <Button type="primary" @click="Submit('AppSecretManage')">提交</Button>
                        <Button @click="Reset('AppSecretManage')" style="margin-left: 108px">重置</Button>
                    </Col>
                </Row>
            </Form>
        </Col>
    </Row>
</template>
<script>
import { mapState } from "vuex";
import { CreateCode,GetOne,Create,Update } from '@/axios/api/AppSecretManageAPI'

export default {
    computed:{
        ...mapState('appsecretmanage',[
            'currentAppSecretManageID',
            'pageIndex',
            'pageSize',
        ])
    },
    components: {
        
    },
    mounted: function(){
        //console.log(this.currentAppSecretManageID)
        if(this.currentAppSecretManageID != ''){
            this.$Spin.show();
        }
        setTimeout(() => {
            this.$Spin.hide();
        }, 1000);

        if(this.currentAppSecretManageID != ''){
            GetOne(this.currentAppSecretManageID).then(res => {
                this.AppSecretManage.Id = res.Id;
                this.AppSecretManage.Num = res.Num;
                this.AppSecretManage.NumCode = res.NumCode;
                this.AppSecretManage.Name = res.Name;
                this.AppSecretManage.AppName = res.AppName;
                this.AppSecretManage.AgentID = res.AgentID;
                this.AppSecretManage.AppKey = res.AppKey;
                this.AppSecretManage.AppSecret =  res.AppSecret;
                this.AppSecretManage.IsUsed = res.IsUsed;
                this.AppSecretManage.CreateTime = res.CreateTime;
            });
        }
        else{
            CreateCode().then(res => {
                this.AppSecretManage.NumCode = res.toString();
            })
        }
    },
    data () {
        return {
            AppSecretManage: {
                Id: '00000000-0000-0000-0000-000000000000',
                Num: 1,
                NumCode: '',
                Name: '',
                AppName: '',
                AgentID: '',
                AppKey: '',
                AppSecret: '',
                IsUsed: 1 ,
                CreateTime: new Date()
            },
            ruleValidate: {
                Name: [{ required: true, message: 'App供应商为必填项！', trigger: 'blur'  }],
                AppName: [{ required: true, message: '应用名称为必填项！', trigger: 'blur'  }],
                AgentID: [{ required: true, message: 'AgentID为必填项！', trigger: 'blur'  }],
                AppKey: [{ required: true, message: 'AppKey为必填项！', trigger: 'blur'  }],
                AppSecret: [{ required: true, message: 'AppSecret为必填项！', trigger: 'blur'  }],
            },
            IsUsed:[
                {
                    value: 1,
                    label: "启用"
                },
                {
                    value: 0,
                    label: "禁用"
                }
            ],
            Parents: [{
                value: '00000000-0000-0000-0000-000000000000',
                label: '根应用'
            }],
            callmodal: "appsecretmanage-sheet",
        }
    },
    watch: {
        /* 'AppSecretManage.Name'(value){
            if(value != '' && value != null){
                this.AccountTail = value.substr(value.length-4,4);
            }
        } */
    },
    methods: {
        Submit (name){
            //this.$Message.success('按了OK键');
             this.$refs[name].validate((valid) => {
                if (valid) {
                    if(this.AppSecretManage.Id == '00000000-0000-0000-0000-000000000000'){
                        this.$Message.success('校验通过!');
                        Create(this.AppSecretManage).then(res => {
                            if(res > 0){

                                setTimeout(() => {
                                    this.$store.dispatch('appsecretmanage/UpdateTableAsync',{
                                        PageIndex: this.pageIndex,
                                        PageSize: this.pageSize
                                    });

                                    this.$Notice.success({
                                        title: '操作提示',
                                        desc: '保存成功！ '
                                    });
                                }, 1000)
                               
                            }
                            else{
                                this.$Notice.error({
                                    title: '操作提示',
                                    desc: '保存失败！ '
                                });
                            }
                        }).catch(err =>{
                            this.$Notice.error({
                                title: '操作提示',
                                desc: '保存失败：' + err + '!'
                            });
                        })
                        
                    }
                    else{
                        this.$Message.success('校验通过!');
                        
                        Update(this.AppSecretManage).then(res => {
                            if(res > 0){
                                setTimeout(() => {
                                    this.$store.dispatch('appsecretmanage/UpdateTableAsync',{
                                        PageIndex: this.pageIndex,
                                        PageSize: this.pageSize
                                    });

                                     this.$Notice.success({
                                        title: '操作提示',
                                        desc: '更新成功！'
                                    });
                                }, 1000)
                            }
                            else{
                                this.$Notice.success({
                                    title: '操作提示',
                                    desc: '更新失败！'
                                });
                            }
                        }).catch(err =>{
                            this.$Notice.error({
                                title: '操作提示',
                                desc: '更新失败：' + err.data + '!'
                            });
                        })
                    }
                    this.ChangeModal();
                } else {
                    this.$Message.error('校验失败!');
                    //this.ChangeModal('AppSecretManage');
                }
            })
            
        },
        Reset (name){
            this.$refs[name].resetFields();
        },
        ChangeModal (){
            //this.$refs[name].resetFields();
            this.$store.commit('appsecretmanage/CHANGE_MODAL');
            this.$router.push('/system/AppSecretManage')
        }
    }
}
</script>
